#include "musicslider.h"
#include "ui_musicslider.h"

#include <QMouseEvent>
#include <QMoveEvent>
#include <QDebug>

MusicSlider::MusicSlider(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MusicSlider)
{
    ui->setupUi(this);

//    ui->lineBox->installEventFilter(this);
//    ui->sliderBox->installEventFilter(this);
//    ui->sliderBtn->setStyleSheet("#sliderBtn { background-color: transparent; }");

    ui->sliderBtn->hide();
}

MusicSlider::~MusicSlider()
{
    delete ui;
}

void MusicSlider::setStep(float ratio)
{
    currentPos = ui->inLine->width() * ratio + ui->lineBox->x();
    moveSlider();
}

//bool MusicSlider::eventFilter(QObject *watched, QEvent *event)
//{
//    if (ui->lineBox == watched || ui->sliderBox == watched)
//    {
//        // 鼠标按下或者鼠标拖动时，需要调节进度
//        if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseMove)
//        {
//            // 获取鼠标点击时的x坐标
//            int x = ui->lineBox->mapFromGlobal(QCursor::pos()).x();

//            // 验证x的合法性
//            int start = ui->inLine->x();
//            int end = ui->inLine->width() + start;
//            x = x > end ? end : x;
//            x = x < start ? start : x;

//            QRect rect = ui->outLine->geometry();
//            rect.setWidth(x - start);
//            ui->outLine->setGeometry(rect);  // 调整outLine滑动条
//            QPoint point(x + ui->lineBox->x() - ui->sliderBox->width() / 2, ui->sliderBox->y());
//            ui->sliderBox->move(point);  // 调整按钮
//        }
//        else if (event->type() == QEvent::Enter) ui->sliderBtn->setStyleSheet("#sliderBtn { background-color: qradialgradient(spread:pad, cx:0.488764, cy:0.505136, radius:0.5, fx:0.337034, fy:0.358, stop:0 rgba(0, 222, 120, 255), stop:0.786517 rgba(30, 204, 136, 255), stop:1 rgba(1, 191, 143, 255)); }");
//        else if (event->type() == QEvent::Leave) ui->sliderBtn->setStyleSheet("#sliderBtn { background-color: transparent; }");
//        return true;
//    }
//    return QObject::eventFilter(watched, event);
//}

void MusicSlider::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        currentPos = event->pos().x();
        moveSlider();
        emit setMusicSliderPosition((float)ui->outLine->width() / ui->inLine->width());
    }
}

void MusicSlider::mouseMoveEvent(QMouseEvent *event)
{
    // 判断鼠标是否在MusicSlider的窗口内，如果在，才能支持拖拽
    // 检测event->pos()是否在MusicSlider窗口矩形范围内
    QRect musicSliderRect(0, 0, width(), height());
    if (!musicSliderRect.contains(event->pos()))
        return;

    if (event->buttons() == Qt::LeftButton)
    {
        currentPos = event->pos().x();

        // 验证currentPos的合法性
        int start = ui->lineBox->x();
        int end = ui->lineBox->width() + start;
        currentPos = currentPos > end ? end : currentPos;
        currentPos = currentPos < start ? start : currentPos;

        moveSlider();
        emit setMusicSliderPosition((float)ui->outLine->width() / ui->inLine->width());
    }
}

void MusicSlider::moveSlider()
{
    QRect rect = ui->outLine->geometry();
    rect.setWidth(currentPos - ui->lineBox->x());
    ui->outLine->setGeometry(rect);  // 调整outLine滑动条
    QPoint point(currentPos - ui->sliderBox->width() / 2, ui->sliderBox->y());
    ui->sliderBox->move(point);  // 调整按钮
}

void MusicSlider::enterEvent(QEvent *event)
{
    (void)event;
    ui->sliderBtn->show();
}

void MusicSlider::leaveEvent(QEvent *event)
{
    (void)event;
    ui->sliderBtn->hide();
}
